var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/area-linkage/index.tsx
var area_linkage_exports = {};
__export(area_linkage_exports, {
  AreaLinkage: () => AreaLinkageWithHoc
});
module.exports = __toCommonJS(area_linkage_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_util = require("../../util");
var useAreaLinkageOptions = (country) => {
  const [options, setOptions] = (0, import_react.useState)([]);
  const areaCacheRef = (0, import_react.useRef)({});
  const loadCountryPromise = (0, import_react.useRef)();
  (0, import_react.useEffect)(() => {
    if (!areaCacheRef.current[country]) {
      loadCountryPromise.current = fetchOptions(country.toUpperCase()).then((res) => {
        areaCacheRef.current = {
          [country]: true
        };
        const _options = transformOptions(res);
        loadCountryPromise.current = void 0;
        setOptions(_options);
        return _options;
      });
    }
  }, [country]);
  const loadData = (0, import_util.useRefCallback)(async (selectedValues) => {
    var _a, _b;
    if (selectedValues.length === 0) {
      return;
    }
    if (selectedValues.every((value) => areaCacheRef.current[value])) {
      return;
    }
    let _options = options;
    if (loadCountryPromise.current) {
      _options = await Promise.resolve(loadCountryPromise.current);
    }
    let currentOptions = _options;
    for await (const value of selectedValues) {
      if (!areaCacheRef.current[value]) {
        const pid = (_a = currentOptions.find((v) => v.value === value)) == null ? void 0 : _a.origin.dataNo;
        if (pid) {
          const res = await fetchOptions(pid);
          const isLeaf = selectedValues[1] === value;
          const nextOptions = transformOptions(res, isLeaf);
          currentOptions == null ? void 0 : currentOptions.forEach((_option, i) => {
            if (_option.value === value) {
              currentOptions[i] = {
                ..._option,
                children: nextOptions
              };
            }
          });
          areaCacheRef.current[value] = true;
        }
      }
      currentOptions = ((_b = currentOptions == null ? void 0 : currentOptions.find((v) => v.value === value)) == null ? void 0 : _b.children) || [];
    }
    setOptions(import_util.iup.deepCopyEx(_options));
  });
  return {
    options,
    loadData
  };
};
var defaultProps = {
  allowClear: true,
  placeholder: "请选择"
};
var AreaLinkage = ({
  country = "CN",
  outRef,
  observer,
  value,
  defaultValue,
  onChange,
  onDropdownVisibleChange,
  ...rest
}) => {
  const cascaderRef = (0, import_react.useRef)(null);
  const [open, setOpen] = (0, import_react.useState)(false);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [innerValue, setInnerValue] = (0, import_react.useState)(value || defaultValue || []);
  const { options, loadData } = useAreaLinkageOptions(country.toUpperCase());
  (0, import_util.useExtendRef)(outRef, {
    getValue() {
      return innerValue;
    },
    setValue(v) {
      if (value === v || innerValue === v) {
        return;
      }
      setInnerValue(v);
      onChange == null ? void 0 : onChange(v, []);
    },
    focus: () => {
      var _a;
      return (_a = cascaderRef.current) == null ? void 0 : _a.focus();
    },
    blur: () => {
      var _a;
      return (_a = cascaderRef.current) == null ? void 0 : _a.blur();
    }
  });
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Cascader,
    {
      ...defaultProps,
      ...rest,
      open,
      loading,
      value: value || innerValue,
      onDropdownVisibleChange: async (open2) => {
        if (open2) {
          setLoading(true);
          await loadData(value || innerValue);
          setLoading(false);
        }
        setOpen(open2);
        onDropdownVisibleChange && onDropdownVisibleChange(open2);
      },
      onChange: (values, selectedOption) => {
        setInnerValue(values);
        onChange && onChange(values, selectedOption);
      },
      loadData: (options2) => loadData(options2.map((v) => v.value)),
      options,
      ref: cascaderRef
    }
  );
};
var AreaLinkageWithHoc = (0, import_util.compHoc)(AreaLinkage, "AreaLinkage");
function transformOptions(data, isLeaf = false) {
  return data.map((item) => {
    return {
      value: item.dataName,
      label: item.dataName,
      children: [],
      disabled: false,
      isLeaf,
      origin: item
    };
  });
}
async function fetchOptions(pid) {
  try {
    const { code, data, message } = await import_util.iup.request.get({
      url: `engine/simpleData/data/getSubSimpleDataList`,
      data: {
        typeCode: "area",
        treePid: pid
      }
    });
    if (code === 0) {
      return data;
    }
    import_util.iup.alert(message);
    return [];
  } catch (e) {
    import_util.iup.alert(e.message);
    return [];
  }
}
(0, import_util.registerComponent)({ AreaLinkage });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AreaLinkage
});
